package com.fooddelivery.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fooddelivery.Common.queryParam.QueryPageParam;
import com.fooddelivery.Common.Result;
import com.fooddelivery.Entity.User;
import com.fooddelivery.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sunwenyuan
 * @since 2025-08-29
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private  UserService userService;

    //查询所有用户user表的信息
//    @GetMapping("/list")
//    public List<User> list(){
//        return userService.list();
//    }

    //boolean类型用来返回是否成功
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.suc("添加成功");
    }

    //查询
    @PostMapping("/login")
    public Result login(@RequestBody User user){
         if (userService.login(user)!=null){
             System.out.println(userService.login(user));
             return Result.sucMsg("登录成功",userService.login(user));
         }
         return Result.fail();
    }
    //注册
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if (userService.save(user)){
            return Result.suc("注册成功");
        }
        return Result.fail();
    }
    //修改(id)
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //删除
    @GetMapping("/delete")
    public boolean delete(Integer id){
        return userService.removeById(id);
    }
    @PostMapping("/listPage")
    //@RequestBody 作用：将json数据映射为对象
    //或者直接用map接收
    public Result listPage(@RequestBody QueryPageParam queryPageParam){
        Page<User> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = queryPageParam.getUser();
        if (StringUtils.hasText(user.getNickname())) {
            lambdaQueryWrapper.like(User ::getNickname, user.getNickname());
        }
        if (StringUtils.hasText(user.getPhone())){
            lambdaQueryWrapper.eq(User ::getPhone,user.getPhone());
        }
        if (user.getRoleId() != null){
            lambdaQueryWrapper.eq(User ::getRoleId,user.getRoleId());
        }
        if (user.getStatus() != null){
            lambdaQueryWrapper.eq(User ::getStatus,user.getStatus());
        }
        IPage result=userService.page(page,lambdaQueryWrapper);

        System.out.println("总查询数据"+result.getTotal());
        System.out.println("当前页数据"+result.getRecords().size());
        return Result.suc(result.getRecords(), result.getTotal());
    }

//    @PostMapping("/listP")
//    //查询（模糊查询，匹配）（名字）
//    public List<User> listP(@RequestBody User user){
//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//        if (StringUtils.hasText(user.getUsername())) {
//            lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
//        }
//
//        if (StringUtils.hasText(user.getNickname())) {
//            lambdaQueryWrapper.like(User::getNickname, user.getNickname());
//        }
//
//        if (user.getAge() != null) {
//            lambdaQueryWrapper.eq(User::getAge, user.getAge());
//        }
//
//        if (StringUtils.hasText(user.getPhone())) {
//            lambdaQueryWrapper.like(User::getPhone, user.getPhone());
//        }
//
//        lambdaQueryWrapper.orderByDesc(User::getId);
//        return userService.list(lambdaQueryWrapper);
//    }



//    @PostMapping("/listPageC")
//    //@RequestBody 作用：将json数据映射为对象
//    //或者直接用map接收
//    public Result listPageC(@RequestBody QueryPageParam queryPageParam){
//        Page<User> page = new Page();
//        page.setCurrent(queryPageParam.getPageNum());
//        page.setSize(queryPageParam.getPageSize());
//
////        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
////        if (StringUtils.hasText(queryPageParam.getUser().getName())) {
////            lambdaQueryWrapper.like(User ::getName, queryPageParam.getUser().getName());
////        }
//        //如果自己要编写查询条件，则需要自己编写
//        IPage result=userService.pageC(page);
////        IPage result=userService.page(page);
////        System.out.println(result.getTotal());
////        System.out.println(result.getRecords().size());
//        return Result.suc(result.getRecords(), result.getTotal());
//    }
}
